{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6cb0fb42-4770-4678-958f-eb8876d427a1",
   "metadata": {},
   "source": [
    "# Evaluate Q&A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b090aeb-35c4-4038-96ea-fa72ff7b00ff",
   "metadata": {},
   "source": [
    "| | |\n",
    "|----------|-------------|\n",
    "| Author(s)   | Renato Leite (renatoleite@), Egon Soares (egon@) |\n",
    "| Last updated | 09/05/2023 |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fd7f606-f779-4b4c-a329-852df5414bea",
   "metadata": {},
   "source": [
    "## Exact Match"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "579f1626-8dfa-4838-8a6f-efd57e3ba89a",
   "metadata": {},
   "source": [
    "$ EM(Truth, Prediction) =\n",
    "  \\begin{cases}\n",
    "    0       & \\quad \\text{if } Truth \\neq Prediction\\\\\n",
    "    1  & \\quad \\text{if } Truth = Prediction\n",
    "  \\end{cases}\n",
    "$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7c517e7a-69f8-40fb-b7c4-afbdb1789a83",
   "metadata": {},
   "outputs": [],
   "source": [
    "def exact_match(ground_truth:str , answer:str) -> int:\n",
    "    return 1 if ground_truth == answer else 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15bd3fe3-92ae-4a93-b56d-40e7e6004f96",
   "metadata": {},
   "source": [
    "### Case 1 - There is an answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "72a663dd-cda0-4ff1-9c80-82cb860b86fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "ground_truth_1 = \"Google was founded on September 4, 1998, by American computer scientists Larry Page and Sergey Brin while they were PhD students at Stanford University in California.\"\n",
    "answer_1_a = \"\"\n",
    "answer_1_b = \"Google was founded on September 4, 1998, by American computer scientists Larry Page and Sergey Brin while they were PhD students at Stanford University in California.\"\n",
    "answer_1_c = \"Google was founded on September 4, 1998, by American computer scientists Larry Page and Sergey Brin.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "786220dd-1534-4ad4-ada7-f5c26a893f97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ground Truth: Google was founded on September 4, 1998, by American computer scientists Larry Page and Sergey Brin while they were PhD students at Stanford University in California.\n",
      "\n",
      "Answer a: \n",
      "EM: 0\n",
      "\n",
      "Answer b: Google was founded on September 4, 1998, by American computer scientists Larry Page and Sergey Brin while they were PhD students at Stanford University in California.\n",
      "EM: 1\n",
      "\n",
      "Answer c: Google was founded on September 4, 1998, by American computer scientists Larry Page and Sergey Brin.\n",
      "EM: 0\n"
     ]
    }
   ],
   "source": [
    "print(f\"Ground Truth: {ground_truth_1}\")\n",
    "print(f\"\\nAnswer a: {answer_1_a}\\nEM: {exact_match(ground_truth_1, answer_1_a)}\")\n",
    "print(f\"\\nAnswer b: {answer_1_b}\\nEM: {exact_match(ground_truth_1, answer_1_b)}\")\n",
    "print(f\"\\nAnswer c: {answer_1_c}\\nEM: {exact_match(ground_truth_1, answer_1_c)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a67b8e8-622f-49c6-ba99-850bd7f10b68",
   "metadata": {},
   "source": [
    "### Case 2 - There is no answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aea2c2a8-77a2-4ac4-a25a-6604900c6470",
   "metadata": {},
   "outputs": [],
   "source": [
    "ground_truth_2 = \"\"\n",
    "answer_2_a = \"\"\n",
    "answer_2_b = \"Google and YouTube are the two most visited websites worldwide followed by Facebook and Twitter.\"\n",
    "answer_2_c = \"No answer found.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "aececfa4-2e41-4d07-a382-0aec1c3e223a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ground Truth: \n",
      "\n",
      "Answer a: \n",
      "EM: 1\n",
      "\n",
      "Answer b: Google and YouTube are the two most visited websites worldwide followed by Facebook and Twitter.\n",
      "EM: 0\n",
      "\n",
      "Answer c: No answer found.\n",
      "EM: 0\n"
     ]
    }
   ],
   "source": [
    "print(f\"Ground Truth: {ground_truth_2}\")\n",
    "print(f\"\\nAnswer a: {answer_2_a}\\nEM: {exact_match(ground_truth_2, answer_2_a)}\")\n",
    "print(f\"\\nAnswer b: {answer_2_b}\\nEM: {exact_match(ground_truth_2, answer_2_b)}\")\n",
    "print(f\"\\nAnswer c: {answer_2_c}\\nEM: {exact_match(ground_truth_2, answer_2_c)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfa4cc4c-cffb-4a67-9c78-b7f8049346c8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
